/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			CustMdl.cc
  \date			June 2011
  \author		TNick

  \brief		Contains the implementation for CustMdl class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n
 Please read ReadMe.txt and Licence.txt in root folder @n
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QHash>

#include	<base/CC_App.h>

#include	"CustMdl.h"
#include	"actions/cst_actmdl.h"
#include	"toolbars/cst_tlbmdl.h"
#include	"menus/cst_mnumdl.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
CustMdl::CustMdl	(QObject* parent, CC_HDL2D* hd_src, bool b_copy)
	: QAbstractItemModel(parent)
{

	if (b_copy = false)
		{
			/* the caller needs to create a duplicate chain by it's own */
			b_editable = true;
		}
	else
		{
			if (hd_src != NULL)
				{
					itm_lst = *hd_src;
					b_editable = false;
				}
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QModelIndex		CustMdl::index		(int a_row, int a_column,
									 const QModelIndex &parent) const
{
	/* we only have one level of data */
	if (!parent.isValid())
		{
			/* item index in valid range? */
			if ((a_row >= 0) && (a_row < itm_lst.count()))
				{
					return createIndex(a_row,a_column,itm_lst.item(a_row));
				}
		}
	return QModelIndex();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool			CustMdl::removeRows	(int row, int count,
									 const QModelIndex &parent)
{
	if (parent.isValid())
		return false;
	if (!isEditable())
		return false;

	beginRemoveRows(QModelIndex(), row, row+count-1);
	CC_L2D* it_a = NULL;
	for (int i = 0; i < count; i++)
		{
			it_a = static_cast<CC_L2D*>(itm_lst.item(row+i));
			if (it_a != NULL)
				{
					itm_lst.remove(row+i);
					delete it_a;
					it_a = NULL;
				}
		}
	endRemoveRows();
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool			CustMdl::insertRows				(
	int row, int count, const QModelIndex & midx)
{

	if (midx.isValid())
		return false;
	if (!isEditable())
		return false;

	CC_L2D* it_a;
	for (int i = 0; i < count; i++)
		{
			it_a = new_item();
			itm_lst.insert(it_a, row);
		}
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		CustMdl::duplicate_chains(CustMdl* p_actm,
								 CustMdl* p_tlbm, CustMdl* p_mnum)
{

	bool		b_ret = false;
	CC_L2D*		itm_m;
	CstAct*		a_src; CstAct*		a_dst;
	CstTlb*		t_src; CstTlb*		t_dst;
	CstMnu*		m_src; CstMnu*		m_dst;
	QHash<CstAct*, CstAct*>			act_lkup;
	CC_HDL2D*	hd_src;

	/* duplicate the actions first and create a translation table */
	hd_src = CC_App::actionsHeader();
	itm_m = hd_src->first();
	act_lkup.reserve(hd_src->count());
	while (itm_m != NULL)
	{
		a_src = static_cast<CstAct*>(itm_m);
		a_dst = new CstAct();
		a_dst->setProperties(a_src);
		act_lkup[a_src] = a_src;
		p_actm->append(a_dst);
		itm_m = itm_m->next();
	}

	/* duplicate toolbars; use translation table to asign actions */
	hd_src = CC_App::toolbarsHeader();
	itm_m = hd_src->first();
	while (itm_m != NULL)
	{
		t_src = static_cast<CstTlb*>(itm_m);
		t_dst = new CstTlb();
		t_dst->setProperties(t_src,act_lkup);
		p_tlbm->append(t_dst);
		itm_m = itm_m->next();
	}

	/* duplicate menus; use translation table to asign actions */
	hd_src = CC_App::menusHeader();
	itm_m = hd_src->first();
	while (itm_m != NULL)
	{
		m_src = static_cast<CstMnu*>(itm_m);
		m_dst = new CstMnu();
		m_dst->setProperties(m_src,act_lkup);
		p_mnum->append(m_dst);
		itm_m = itm_m->next();
	}

	return b_ret;
}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */

